package com.heima.feign.clients.fallback;

import com.heima.feign.clients.WemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class WemediaClientFallbackFactory implements FallbackFactory<WemediaClient> {
    @Override
    public WemediaClient create(Throwable throwable) {

        log.info("即将调用 WemediaClient 远程调用的服务降级策略...");

        /*
            服务降级策略：仅仅记录异常信息，返回友好的错误信息提示，而没有做真正的服务降级策略。
         */
        return new WemediaClient() {
            @Override
            public ResponseResult<WmUser> save(WmUser wmUser) {
                log.info("请求参数: {}", wmUser);
                log.info("执行远程调用时,WemediaClientFallbackFactory save 出现异常: {}", throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult<WmUser> findByName(String name) {
                log.info("请求参数: {}", name);
                log.info("执行远程调用时,WemediaClientFallbackFactory save 出现异常: {}", throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult queryWmNewsById(Integer id) {
                log.error("文章发布业务-远程调用-根据 newsId 查询 WmNews 信息失败，失败原因: {}, 参数: {}", throwable.getMessage(), id);
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult updateWmNewsById(WmNews wmNews) {
                log.error("文章发布业务-远程调用-修改 WnNews 的信息失败，失败原因: {}, 参数: {}", throwable.getMessage(), wmNews);
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }
        };
    }
}
